﻿Public Class ThemHD
    Private xlhd As XLHopDong
    Private dshd As New ArrayList
    Private dscv As New ArrayList
    Private dsht As New ArrayList
    Private dsth As New ArrayList
    Private dsnv As List(Of DTO_NhanVienCT.NhanVien)
    Public madd As String
    Public mahd As String
    Private data As DataTable
    Private dataCT As DataTable
    Private dataNV As DataTable

    Private Sub ThemHD_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        xlhd = New XLHopDong
        data = New DataTable
        dataCT = New DataTable
        dataNV = New DataTable
        Dim xlnv As New XLNhanVien
        dshd = xlhd.LoaiHD
        dscv = xlhd.DSCV
        dsht = xlhd.BUS_HTTra
        dsth = xlhd.BUS_THHD
        dataCT = xlhd.BUS_DSCTHD
        data = xlhd.BUS_AllDSHD
        dataNV = xlnv.BUS_LayDSNV
        XuatCB()
        If HopDongLD.dau = False Then
            bt_them_thoat.Enabled = False
            data = xlhd.BUS_DHDDK(mahd)
            showupdate(data)
        Else
            bt_luu_thoat.Enabled = False
            dsnv = New List(Of DTO_NhanVienCT.NhanVien)
            dsnv = xlhd.BUS_ListDSNV(HopDongLD.manv)
            'dsqt = xlhd.DSQT()
            tb_luongCB.Text = xlhd.LayLuongCB()
            tb_ngaytra.Text = xlhd.BUS_NgTrLuong()
            showform()
            tb_maHD.Text = xlhd.BUS_MAHD()
        End If
        tb_daidien.Text = "CTy TNHH Tri Thức Trẻ Bình Minh"
    End Sub
    Private Sub XuatCB()
        cb_LoaiHD.Items.Clear()
        cb_chucvu.Items.Clear()
        cb_htLuong.Items.Clear()
        cb_thoihan.Items.Clear()
        For Each s As String In dshd
            cb_LoaiHD.Items.Add(s)
        Next
        For Each s As String In dsht
            cb_htLuong.Items.Add(s)
        Next
        For Each s As String In dsth
            cb_thoihan.Items.Add(s)
        Next
        cb_thoihan.SelectedIndex = 0
        cb_LoaiHD.SelectedIndex = 0
        cb_htLuong.SelectedIndex = 0
        cb_chucvu.DataSource = dscv
        cb_chucvu.DisplayMember = "TenChucVu"
    End Sub

    Private Sub showform()
        tb_hotenB.Text = dsnv.Item(0).HoTen
        tb_QT1.Text = dsnv.Item(0).QuocTich
        tb_ngaysinh.Text = dsnv.Item(0).NgaySinh
        tb_cmnd.Text = dsnv.Item(0).CMND
        tb_NgayCap.Text = dsnv.Item(0).NgayCap
        tb_nccmnd.Text = dsnv.Item(0).NoiCap
        tb_dcTru.Text = dsnv.Item(0).DiaChi
    End Sub
    Private Sub showupdate(ByVal data As DataTable)
        xlhd = New XLHopDong
        dsnv = New List(Of DTO_NhanVienCT.NhanVien)
        For Each dr As DataRow In data.Rows
            dsnv = xlhd.BUS_ListDSNV(dr("MaNV"))
        Next
        tb_tenA.Text = dsnv.Item(0).HoTen
        tb_QT.Text = dsnv.Item(0).QuocTich
        tb_cvu.Text = dsnv.Item(0).ChucVu
        tb_dthoai.Text = dsnv.Item(0).DienThoai
        tb_dchi.Text = dsnv.Item(0).DiaChi
        dsnv = New List(Of DTO_NhanVienCT.NhanVien)
        For Each dr As DataRow In data.Rows
            dsnv = xlhd.BUS_ListDSNV(xlhd.BUS_LayMaNV(dr("MaHD")))
        Next
        tb_hotenB.Text = dsnv.Item(0).HoTen
        tb_QT1.Text = dsnv.Item(0).QuocTich
        tb_ngaysinh.Text = dsnv.Item(0).NgaySinh
        tb_cmnd.Text = dsnv.Item(0).CMND
        tb_NgayCap.Text = dsnv.Item(0).NgayCap
        tb_nccmnd.Text = dsnv.Item(0).NoiCap
        tb_dcTru.Text = dsnv.Item(0).DiaChi
        tb_maHD.Text = data.Rows(0).Item("MaHD").ToString()
        tb_nghenghiep.Text = data.Rows(0).Item("NNghiep").ToString()
        tb_soLD.tb_nhapso.Text = data.Rows(0).Item("SoLD").ToString()
        dt_ncLD.Text = data.Rows(0).Item("NgCap").ToString()
        tb_ncld.Text = data.Rows(0).Item("NCap").ToString()
        cb_LoaiHD.Text = data.Rows(0).Item("LoaiHD").ToString()
        cb_thoihan.Text = data.Rows(0).Item("THan").ToString()
        dt_NgayKy.Text = data.Rows(0).Item("NgKy").ToString()
        dt_NgayBD.Text = data.Rows(0).Item("NgBD").ToString()
        dt_ngayKT.Text = data.Rows(0).Item("NgKT").ToString()
        dt_TV.Text = data.Rows(0).Item("NgBDTV").ToString()
        dt_KTTV.Text = data.Rows(0).Item("NgKTTV").ToString()
        tb_chucDanh.Text = data.Rows(0).Item("CDanh").ToString()
        cb_chucvu.Text = data.Rows(0).Item("CVu").ToString()
        tb_congviec.Text = data.Rows(0).Item("CViec").ToString()
        tb_diadiem.Text = data.Rows(0).Item("DCLam").ToString()
        tb_luongCB.Text = data.Rows(0).Item("LuongCB").ToString()
        cb_htLuong.Text = data.Rows(0).Item("TrLuong").ToString()
        tb_ngaytra.Text = data.Rows(0).Item("NgTra").ToString()
        tb_phucap.Text = data.Rows(0).Item("PCap").ToString()
        tb_BH.Text = data.Rows(0).Item("BHiem").ToString()
        tb_glViec.Text = data.Rows(0).Item("GLam").ToString()
        tb_tyeu.Text = data.Rows(0).Item("TrYeu").ToString()
        tb_ghichu.Text = data.Rows(0).Item("GChu").ToString()
        check_l.Checked = False
        If data.Rows(0).Item("TTrang").ToString() = "True" Then
            check_l.Checked = True
        End If
    End Sub
    Private Sub bt_luu_thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_luu_thoat.Click
        xlhd = New XLHopDong
        Dim h As New DTO_HopDong.HopDong
        If LayDL(h, 0) = False Then
            For Each dr As DataRow In data.Rows
                If dr("MaHD") <> "" Then
                    For Each dr1 As DataRow In dataCT.Rows
                        If dr("MaHD") = dr1("MaHD") Then
                            xlhd.BUS_ADD_UPHD(dr, dr1, h)
                            xlhd.BUS_SaveHD(data)
                            xlhd.BUS_SaveHDCT(dataCT)
                            Exit For
                        End If
                    Next
                    Exit For
                End If
            Next
        Else
            MessageBox.Show("Dữ Liệu Nhập Vào Sai Hoặc Thiếu", "Thông Báo", MessageBoxButtons.OK)
        End If
        Me.Close()
    End Sub

    Private Sub bt_them_thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_them_thoat.Click
        Dim tvds As Windows.Forms.ListView = Me.Tag
        xlhd = New XLHopDong
        Dim h As New DTO_HopDong.HopDong
        If LayDL(h, 1) = False Then
            Dim dr As DataRow = data.NewRow 'luu vao hd
            Dim dct As DataRow = dataCT.NewRow 'luu vao hd chi tiet
            xlhd.BUS_ADD_UPHD(dr, dct, h)
            data.Rows.Add(dr)
            dataCT.Rows.Add(dct)
            xlhd.BUS_SaveHD(data)
            xlhd.BUS_SaveHDCT(dataCT)
            Me.Close()
        Else
            MessageBox.Show("Dữ Liệu Nhập Vào Sai Hoặc Thiếu", "Thông Báo", MessageBoxButtons.OK)
        End If
    End Sub

    Private Sub bt_thoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_thoat.Click
        Me.Close()
    End Sub

    Private Sub bt_Lamlai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt_Lamlai.Click
        Refesh_()
    End Sub
    Private Sub Refesh_()
        xlhd = New XLHopDong
        tb_maHD.Text = xlhd.BUS_MAHD()
        'tb_tenA.Text = String.Empty
        tb_nghenghiep.Text = String.Empty
        tb_soLD.tb_nhapso.Text = String.Empty
        'dt_capcmnd.Text = DateTime.Now
        dt_ncLD.Text = DateTime.Now
        'tb_nccmnd.Text = String.Empty
        tb_ncld.Text = String.Empty
        'tb_dcTru.Text = String.Empty
        check_l.Checked = False
        cb_thoihan.SelectedIndex = 0
        dt_NgayKy.Text = DateTime.Now
        dt_TV.Text = DateTime.Now
        dt_NgayBD.Text = DateTime.Now
        dt_KTTV.Text = DateTime.Now
        dt_ngayKT.Text = DateTime.Now
        tb_chucDanh.Text = String.Empty
        tb_congviec.Text = String.Empty
        tb_diadiem.Text = String.Empty
        'tb_luongCB.Text = String.Empty
        cb_htLuong.SelectedIndex = 0
        'tb_ngaytra.Text = String.Empty
        tb_phucap.Text = String.Empty
        tb_BH.Text = String.Empty
        tb_glViec.Text = String.Empty
        tb_tyeu.Text = String.Empty
        tb_ghichu.Text = String.Empty
        cb_chucvu.SelectedIndex = 0
        'cb_chucvu1.SelectedIndex = 0
        'cb_quoctich.SelectedIndex = 0
        'cb_quoctich1.SelectedIndex = 0
        cb_LoaiHD.SelectedIndex = 0
    End Sub
    Private Function LayDL(ByRef h As DTO_HopDong.HopDong, ByVal dau As Integer) As Boolean
        xlhd = New XLHopDong
        If HopDongLD.dau = True Then
            h.MaHD = tb_maHD.Text
        Else
            h.MaHD = HopDongLD.mahd
        End If
        h.MaNVDD = madd
        h.MaNV = HopDongLD.manv
        h.NNghiep = tb_nghenghiep.Text
        h.SoLD = 0
        If tb_soLD.tb_nhapso.Text <> "" Then
            h.SoLD = Convert.ToUInt32(tb_soLD.tb_nhapso.Text)
        End If
        h.NgCap = dt_ncLD.Text
        h.NCap = tb_ncld.Text
        h.TenHD = cb_LoaiHD.Text
        h.THan = 0
        If cb_thoihan.Text <> "" Then
            h.THan = cb_thoihan.Text
        End If
        h.NgKy = dt_NgayKy.Text
        h.NgHLuc = dt_NgayBD.Text
        h.NgKT = dt_ngayKT.Text
        h.NgBDTV = dt_TV.Text
        h.NgKTTV = dt_KTTV.Text
        h.CDanh = tb_chucDanh.Text
        h.Cvu = cb_chucvu.Text
        h.CViec = tb_congviec.Text
        h.DCLam = tb_diadiem.Text
        h.LCB = Convert.ToUInt32(tb_luongCB.Text)
        h.TrLuong = cb_htLuong.Text
        h.NgTra = Convert.ToUInt32(tb_ngaytra.Text)
        h.PCap = tb_phucap.Text
        h.BHiem = tb_BH.Text
        h.GLam = tb_glViec.Text
        h.TrYeu = tb_tyeu.Text
        h.GChu = tb_ghichu.Text
        h.TTrang = False
        If check_l.Checked = True Then
            h.TTrang = True
        End If
        Return xlhd.BUS_KtraThem(h, dataCT, dau)
    End Function
    Private Sub ThemHD_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim dr As DialogResult = MessageBox.Show("Bạn có muốn lưu dữ liệu lại trước khi đóng màn hình này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If dr = Windows.Forms.DialogResult.Yes Then
            xlhd = New XLHopDong
            If xlhd.BUS_SaveHD(data) = True Then
                MessageBox.Show("Lưu Thành Công ", "Thông Báo", MessageBoxButtons.OK)
            End If
        End If
        HopDongLD.state = False
    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        Dim nv As New NhanVien2
        nv.ShowDialog()
        Dim n As Integer = nv.ds.Count
        For Each s As DTO_NhanVienCT.NhanVien In nv.ds
            madd = s.MaNV
            tb_tenA.Text = s.HoTen
            tb_QT.Text = s.QuocTich
            tb_cvu.Text = s.ChucVu
            tb_dchi.Text = s.DiaChi
            tb_dthoai.Text = s.DienThoai
        Next
    End Sub
End Class
'h.MaNVDD =
'           h.MaNV =
'           h.NNghiep =
'           h.SoLD =
'           h.NgCap =
'           h.NCap =
'           h.TenHD =
'           h.THan
'       h.NgKy
'       h.NgHLuc
'       h.NgKT
'       h.NgBDTV
'       h.NgKTTV
'       h.CDanh
'       h.Cvu
'       h.CViec
'       h.DCLam
'       h.LCB()
'       h.TrLuong
'       h.NgTra()
'       h.PCap
'       h.BHiem
'       h.GLam
'       h.GChu
'       h.TTrang()